package com.bank.ui;

import com.bank.ui.customer.CustomerDashboard;
import com.bank.service.AuthService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CustomerLoginUI extends JFrame {
    private JTextField nameField;
    private JTextField pidField;

    public CustomerLoginUI() {
        setTitle("客户登录");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout(10, 10));
        setLocationRelativeTo(null);

        // 标题
        JLabel titleLabel = new JLabel("客户登录", SwingConstants.CENTER);
        titleLabel.setFont(new Font("宋体", Font.BOLD, 20));
        add(titleLabel, BorderLayout.NORTH);

        // 表单面板
        JPanel formPanel = new JPanel(new GridLayout(2, 2, 10, 10));
        formPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        formPanel.add(new JLabel("姓名:"));
        nameField = new JTextField();
        formPanel.add(nameField);

        formPanel.add(new JLabel("身份证号:"));
        pidField = new JTextField();
        formPanel.add(pidField);

        add(formPanel, BorderLayout.CENTER);

        // 按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));

        // 登录按钮
        JButton loginButton = new JButton("登录");
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                login();
            }
        });
        buttonPanel.add(loginButton);

        // 注册按钮
        JButton registerButton = new JButton("注册");
        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                openCustomerRegister();
            }
        });
        buttonPanel.add(registerButton);

        // 返回按钮
        JButton backButton = new JButton("返回");
        backButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                openIdentitySelection();
            }
        });
        buttonPanel.add(backButton);

        add(buttonPanel, BorderLayout.SOUTH);
    }

    private void login() {
        String name = nameField.getText().trim();
        String pid = pidField.getText().trim();

        if (name.isEmpty() || pid.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请输入姓名和身份证号！", "输入错误", JOptionPane.WARNING_MESSAGE);
            return;
        }

        // 调用AuthService进行客户登录验证
        int customerId = AuthService.customerLogin(name, pid);
        if (customerId > 0) {
            JOptionPane.showMessageDialog(this, "登录成功！");
            try {
                // 直接使用表单中的身份证号作为userPID参数
                openCustomerDashboard(customerId, pid); // 使用pid作为第二个参数
            } catch (Exception ex) {
                ex.printStackTrace();
                JOptionPane.showMessageDialog(this,
                        "打开用户面板失败: " + ex.getMessage(),
                        "错误", JOptionPane.ERROR_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(this, "登录失败：信息错误！", "登录失败", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void openCustomerDashboard(int customerId, String userPID) {
        // 确保传递两个参数给 CustomerDashboard 的构造函数
        CustomerDashboard customerDashboard = new CustomerDashboard(String.valueOf(customerId), userPID);
        JFrame dashboardFrame = new JFrame("客户仪表盘");
        dashboardFrame.setContentPane(customerDashboard);
        dashboardFrame.setSize(800, 600);
        dashboardFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        dashboardFrame.setLocationRelativeTo(null);
        dashboardFrame.setVisible(true);
        dispose();
    }

    private void openCustomerRegister() {
        // 传递当前窗口作为父窗口
        RegisterUI registerUI = new RegisterUI(this);
        registerUI.setVisible(true);
        setVisible(false); // 隐藏当前窗口而不是关闭
    }

    private void openIdentitySelection() {
        // 假设IdentitySelectionUI是已实现的身份选择界面
        IdentitySelectionUI identityUI = new IdentitySelectionUI();
        identityUI.setVisible(true);
        dispose();
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(() -> {
            CustomerLoginUI customerLoginUI = new CustomerLoginUI();
            customerLoginUI.setVisible(true);
        });
    }
}